// @ts-nocheck

import CryptoJS from 'crypto-js';
import moment from 'moment';
import { request } from 'umi';
import {get} from "@/services/services";
// import define from "../../config/define";
// import {updatePersonalRule} from "@/pages/settings/service";
// get
export interface OSSOptions {
  region: string;
  bucket: string;
  accessKeyId: string;
  accessKeySecret: string;
  filepath: string;
}
export interface OSSData {
  dir: string;
  expire: string;
  host: string;
  accessId: string;
  policy: string;
  signature: string;
}

const oss =  await get();
const osssdk = oss['data']
// console.log(osssdk['data'],'')
const policyText = {
  expiration: moment()
    .add(1, 'days')
    .utc()
    .format(), //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了

  conditions: [
    ['content-length-range', 0, 10485760000000], // 设置上传文件的大小限制

  ],
};
const Crypto: any = CryptoJS;
const policy = Crypto.enc.Base64.stringify(
  Crypto.enc.Utf8.parse(JSON.stringify(policyText)),
);

const signature = Crypto.HmacSHA1(policy, osssdk.AccessKeySecret).toString(
  Crypto.enc.Base64,
);

const OSS = {
  option: {
    dir: `${OSSOPTIONS.filepath}${moment().format('YYYYMM')}/`,
    expire: '1577811661',
    host: `https://${OSSOPTIONS.bucket}.${OSSOPTIONS.region}.aliyuncs.com/`,
    accessId: osssdk.AccessKeyId,
    policy: policy,
    'x-oss-security-token':osssdk.SecurityToken,
    signature: signature,
  },
  getUploadFileName: (file: RcFile) => {
    const suffix = file.name.slice(file.name.lastIndexOf('.'));
    return `${file.uid}${suffix}`;
  },
  setUploadFileUrl: (file: RcFile) => {
    file.url = `${OSS.option.host}/${OSS.option.dir}${OSS.getUploadFileName(
      file,
    )}`;
    return file;
  },
  transformFile: (file: RcFile, FileList: RcFile[]) => {
    const filename = OSS.getUploadFileName(file);
    console.log('transformFile.filename', filename);
    file.path = OSS.option.dir + filename;
    file.url = OSS.option.host + '/' + OSS.option.dir + filename;
    console.log('transformFile.file', file);
    return file;
  },
  getExtraData: (file: RcFile) => {
      /*  accessKeyId = data.AccessKeyId
          accessKeySecret = data.AccessKeySecret
          stsToken = data.SecurityToken

       */
    return {
      key: `${OSS.option.dir}${OSS.getUploadFileName(file)}`,
      OSSAccessKeyId: osssdk.AccessKeyId,
      'x-oss-security-token':osssdk.SecurityToken,
      policy: OSS.option.policy,
      Signature: OSS.option.signature,
    };
  },
  upload: (file: any) => {
    const transformFile = OSS.transformFile(file);
    const extraData = OSS.getExtraData(file);
    const formData = new FormData();

    for (const key in extraData) {
      if (Object.prototype.hasOwnProperty.call(extraData, key)) {
        formData.append(key, extraData[key]);
      }
    }
    formData.append('file', transformFile, transformFile.name);

    return new Promise((resolve, reject) => {
      request(OSS.option.host, {
        method: 'POST',
        body: formData,
      })
        .then(result => {
          console.log(result,'res')
          console.log(transformFile,'res')
          resolve(transformFile);
        })
        .catch(error => {
          reject({ state_code: 500 });
        });
    });
  },
};

export default OSS;
